package com.ma4567.remotecontroller.MysqlHelper;

import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.UserManager.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Date;
import java.sql.SQLException;

class Log{
    public int OperatorId;
    public String OperatorName;
    public String operation;
    public int TargetId;

    public Log(String operation, String cookie, int target){
        User temp = RemoteControllerApplication.userManager.getUserByCookie(cookie);
        this.TargetId = target;
        this.OperatorId = temp.Account;
        this.OperatorName = temp.name;
        this.operation = operation;
    }

    public Log(String operation, String cookie){
        User temp = RemoteControllerApplication.userManager.getUserByCookie(cookie);
        this.TargetId = -1;
        this.OperatorId = temp.Account;
        this.OperatorName = temp.name;
        this.operation = operation;
    }
}

public class TableLogManager {
    public static void Init() throws SQLException {
        Connection conn = ConnectPool.getConnection();
        PreparedStatement stmt = conn.prepareStatement(
                "create table if not exists log" +
                "(  operator_id int  not null primary key," +
                "   operator_name text not null," +
                "   operation text not null," +
                "   time datetime not null," +
                "   target_id int null," +
                "   constraint target_id foreign key (target_id) references user (Account))" +
                        "    character set utf8 engine InnoDB;");
        stmt.executeUpdate();
        conn.close();
    }

    public static void AddLog(String operation, String cookie, int target) throws SQLException {
        Connection conn = ConnectPool.getConnection();

        Log newLog = new Log(operation, cookie, target);

        PreparedStatement stmt = conn.prepareStatement("insert ignore log(operator_id,operator_name,operation,time,target_id) " +
                "values (?,?,?,?,?)");
        stmt.setInt(1, newLog.OperatorId);
        stmt.setString(2, newLog.OperatorName);
        stmt.setString(3, newLog.operation);
        stmt.setObject(4, new java.sql.Timestamp(new java.util.Date().getTime()));
        stmt.setInt(5, newLog.TargetId);
        stmt.executeUpdate();

        System.out.println("Operation from userid: " + newLog.OperatorId + " name: " + newLog.OperatorName + ". Operation: " + newLog.operation + ". Target: " + newLog.TargetId);

        conn.close();
    }

    public static void AddLog(String operation, String cookie) throws SQLException {
        Connection conn = ConnectPool.getConnection();

        Log newLog = new Log(operation, cookie);

        PreparedStatement stmt = conn.prepareStatement("insert ignore log(operator_id,operator_name,operation,time) " +
                "values (?,?,?,?)");
        stmt.setInt(1, newLog.OperatorId);
        stmt.setString(2, newLog.OperatorName);
        stmt.setString(3, newLog.operation);
        stmt.setObject(4, new java.sql.Timestamp(new java.util.Date().getTime()));
        stmt.executeUpdate();

        System.out.println("Operation from userid: " + newLog.OperatorId + " name: " + newLog.OperatorName + ". Operation: " + newLog.operation + ". Without target");

        conn.close();
    }
}
